﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using Telerik.Reporting.Cache.File;
using Telerik.Reporting.Services;
using Telerik.Reporting.Services.WebApi;
using System.Web.Script.Serialization;
using System.Collections;
using System.Data.SqlTypes;
using ezCloud.Data;
using ezCloud.Api.Common;
using Microsoft.AspNet.Identity.Owin;
using System.Data.SqlTypes;
using ezCloud.Api.Models;
using ezCloud.Data;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace ezCloud.Api.Controllers
{
    [RoutePrefix("api/reports")]
    [Authorize]
    public class ReportsController : ReportsControllerBase
    {
        static ReportServiceConfiguration configurationInstance;

        static ReportsController()
        {
            var appPath = HttpContext.Current.Server.MapPath("~/");
            var reportsPath = Path.Combine(appPath, @"Reports");
            var resolver = new Common.CustomReportResolver();

            /*var resolver = new ReportFileResolver(reportsPath)
                .AddFallbackResolver(new ReportTypeResolver());*/

            configurationInstance = new ReportServiceConfiguration
            {
                HostAppId = "ezCloudReporting",
                Storage = new FileStorage(),
                ReportResolver = resolver,
                // ReportSharingTimeout = 0,
                // ClientSessionTimeout = 15,
            };



        }
        /*class ReportParams
        {
            string _report;
            Hashtable _parameterValues;

            public string report
            {
                get
                {
                    return _report;
                }

                set
                {
                    _report = value;
                }
            }

            public Hashtable parameterValues
            {
                get
                {
                    return _parameterValues;
                }

                set
                {
                    _parameterValues = value;
                }
            }
        }*/
        public ReportsController()
        {
            /* var stream = HttpContext.Current.Request.InputStream;
             StreamReader sr = new StreamReader(stream);
             var str  = sr.ReadToEnd();
             var serializer = new JavaScriptSerializer();
             ReportParams rp = (ReportParams) serializer.Deserialize(str, typeof( ReportParams));
             if (rp != null && rp.report != null)
             {
                 switch (rp.report)
                 {
                     case "ezCloud.Reporting.RoomInvoice":
                         if (rp != null && rp.parameterValues != null && rp.parameterValues["HotelId"] != null)
                         {
                             //check permission
                         }
                         break;
                     default:
                         break;
                 }
             }*/
            this.ReportServiceConfiguration = configurationInstance;
        }
        //[HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
       
    }
    [RoutePrefix("api/configreports")]
    [Authorize]
    public class ConfigReportsController: ezController{
        [HttpGet]
        [AllowAnonymous]
        [Route("GetConfig")]
        public async Task<IHttpActionResult> getConfig()
        {
            var currentUser = Common.UserUtitily.getUserProfile();
            List<FavoriteReport> list = context.FavoriteReport.Where(m => m.HotelId == currentUser.HotelId && m.UserId == currentUser.UserId).ToList();
            return Ok(new { List = list });
        }
        [HttpPost]
        [AllowAnonymous]
        [Route("updateConfig")]
        public async Task<IHttpActionResult> updateConfig(FavoriteReport model)
        {
            var currentUser = Common.UserUtitily.getUserProfile();
            if (model == null)
            {
                return BadRequest("Report_Code_IS_NULL");
            }
            var report = context.FavoriteReport.Where(m => m.HotelId == currentUser.HotelId && m.UserId == currentUser.UserId && m.ReportCode == model.ReportCode).FirstOrDefault();
            //
            if (report == null)
            {
                context.FavoriteReport.InsertOnSubmit(new FavoriteReport()
                {
                    HotelId = currentUser.HotelId,
                    UserId = currentUser.UserId,
                    ReportCode = model.ReportCode
                });
            }
            else
            {
                context.FavoriteReport.DeleteOnSubmit(report);
            }
            context.SubmitChanges();
            return Ok("Success");
        }
    }
}
